import requests

from bs4 import BeautifulSoup
import mysql.connector

def pa(limit):
    print(limit)
    #第几页
    p = limit
    #学校名称
    schoolName = "兰州大学"

    #爬取链接
    url = "https://job.lzu.edu.cn/html/22/article/list/list_"+p+".html"

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)
    html.encoding = "utf-8"

    soup = BeautifulSoup(html.text, 'lxml')

    #获取class="lmain_item_con"的标签内容
    top = soup.find_all(class_ ="lmain_item_con")

    #从top中找到所有li标签
    dataList = top[0].find_all("li")

    for data in dataList:
        createTime = data.find("span").text
        jobTitle = data.find("a").text
        print(createTime+"|"+jobTitle)
        mysql_conn(jobTitle,schoolName,createTime)

#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    for i in range(2,265):
        pa(str(i))
